package com.duqc.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedisUtils
 * @Description TODO
 * @Author duqc
 * @Date 2021/4/14
 **/
@Component
@Configuration
@RequiredArgsConstructor
public class RedisUtil {

    private final RedisTemplate redisTemplate;

    /**
     * 设置redis的key-value
     *
     * @param key
     * @param value
     */
    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置redis的key-value,带过期时间
     *
     * @param key
     * @param value
     * @param time
     */
    public void setString(String key, Object value, Long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    /**
     * 获取redis中key对应的值
     *
     * @param key
     * @return
     */
    public Object getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除redis中key对应的值
     *
     * @param key
     * @return
     */
    public Boolean deleteKey(String key) {
        return redisTemplate.delete(key);
    }

}
